package com.dream.common.utils;

import jakarta.validation.Validation;
import jakarta.validation.Validator;
import jakarta.validation.ValidatorFactory;

/**
 * bean对象属性验证
 *
 * @author hlm
 */
public class ValidationUtils {

    /**
     * 默认{@link Validator} 对象
     */
    private static final Validator validator;

    static {
        try (ValidatorFactory factory = Validation.buildDefaultValidatorFactory()) {
            validator = factory.getValidator();
        }
    }

    /**
     * 校验对象
     *
     * @param <T>    Bean类型
     * @param bean   bean
     * @param groups 校验组
     */
    public static <T> void validate(T bean, Class<?>... groups) {
        validator.validate(bean, groups);
    }
}
